package com.jiangsong.sqlparser;


import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;

public class Parser {

    public static String decoSqlWithTalent(String originSql) {
        try {
            // 解析SQL语句
            Statement statement = CCJSqlParserUtil.parse(originSql);
            // 判断是否为SELECT语句
            if (statement instanceof Select) {
                Select select = (Select) statement;
                // 输出SELECT语句中的表名
                SelectBody selectBody = select.getSelectBody();
                if (selectBody instanceof PlainSelect) {
                    PlainSelect plainSelect = (PlainSelect) selectBody;
                    EqualsTo tenantCondition = new EqualsTo();
                    tenantCondition.setLeftExpression(new Column("tenant_id"));
                    tenantCondition.setRightExpression(new StringValue("cmbs"));
                    Expression where = plainSelect.getWhere();
                    if (where == null) {
                        // 如果原 SQL 没有 WHERE 子句，直接添加租户条件
                        plainSelect.setWhere(tenantCondition);
                    } else {
                        // 如果原 SQL 有 WHERE 子句，添加 AND 条件
                        AndExpression andExpression = new AndExpression(where, tenantCondition);
                        plainSelect.setWhere(andExpression);
                    }
                }
                // 重新生成 SQL 语句
                String modifiedSql = select.toString();

                return modifiedSql;
            }
        } catch (JSQLParserException e) {
            e.printStackTrace();
            throw new RuntimeException("sql解析错误");
        }

        return null;

    }

    public static void main(String[] args) {
        String sql = "SELECT distinct column1, column2 FROM (select column1, column2,column3 from table2) as table1 WHERE table1.column3 > 10";
        try {
            // 解析SQL语句
            System.out.println("111111");
            Statement statement = CCJSqlParserUtil.parse(sql);
            // 判断是否为SELECT语句
            if (statement instanceof Select) {
                Select select = (Select) statement;
                // 输出SELECT语句中的表名
                SelectBody selectBody = select.getSelectBody();
                if (selectBody instanceof PlainSelect) {
                    PlainSelect plainSelect = (PlainSelect) selectBody;
                    EqualsTo tenantCondition = new EqualsTo();
                    tenantCondition.setLeftExpression(new Column("tenant_id"));
                    tenantCondition.setRightExpression(new StringValue("cmbs"));
                    Expression where = plainSelect.getWhere();
                    if (where == null) {
                        // 如果原 SQL 没有 WHERE 子句，直接添加租户条件
                        plainSelect.setWhere(tenantCondition);
                    } else {
                        // 如果原 SQL 有 WHERE 子句，添加 AND 条件
                        AndExpression andExpression = new AndExpression(where, tenantCondition);
                        plainSelect.setWhere(andExpression);
                    }
                }
                // 重新生成 SQL 语句
                String modifiedSql = select.toString();

                System.out.println("Table name: " + modifiedSql);
            }
        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
    }
}
